package ijeoma.tween;

import java.util.ArrayList;
import java.util.Iterator;

import processing.core.PApplet;

abstract class TweenPlugin implements TweenEventListener {
	public static String REVERSE = "reverse";
	public static String NO_REVERSE = "noReverse";

	public static String TWEEN = "tween";
	public static String TWEENGROUP = "tweenGroup";

	public PApplet parent;

	public ArrayList<Object> items;

	public String repeatMode = NO_REVERSE;
	public int repeatCount = 0;
	public int repeatTimes = 0;

	public boolean isTweening = false;
	public boolean isRepeating = false;
	public boolean isDebugging = false;

	protected ArrayList<TweenEventListener> listeners;

	public TweenPlugin(PApplet _parent) {
		parent = _parent;

		items = new ArrayList<Object>();
		listeners = new ArrayList<TweenEventListener>();
	}

	public TweenPlugin(PApplet _parent, Object[] _items) {
		parent = _parent;

		items = new ArrayList<Object>();
		listeners = new ArrayList<TweenEventListener>();

		for (int i = 0; i < _items.length; i++) {
			Object currentObject = _items[i];

			if (currentObject.getClass().getSimpleName().equals("Tween")) {
				((Tween) currentObject).addEventListener(this);
				items.add(currentObject);
			} else {
				((TweenGroup) currentObject).addEventListener(this);
				items.add(currentObject);
			}
		}
	}

	public void addTween(Tween _tween) {
		_tween.addEventListener(this);
		items.add(_tween);
	}

	public void addTweens(Tween[] _tweens) {
		for (int i = 0; i < _tweens.length; i++) {
			addTween(_tweens[i]);
		}
	}

	public void addTweenGroup(TweenGroup _tweenGroup) {
		_tweenGroup.addEventListener(this);
		items.add(_tweenGroup);
	}

	public void addItem(Object _tween) {
		if (_tween.getClass().getSimpleName().equals("Tween"))
			addTween((Tween) _tween);
		else
			addTweenGroup((TweenGroup) _tween);
	}
	
	public void addItems(Object[] _tweens) {
		for (int i = 0; i < _tweens.length; i++) {
			addItem(_tweens[i]);
		}
	}
	
	public int countItems() {
		return items.size();
	}

	public boolean removeTween(Tween _tween) {
		items.remove(_tween);
		return false;
	}

	public boolean removeTweenGroup(TweenGroup _tweenGroup) {
		items.remove(_tweenGroup);
		return false;
	}
	
	public boolean removeItem(Object _item) {
		items.remove(_item);
		return false;
	}

	public void repeat() {
		isRepeating = true;
	}

	public void repeat(String _repeatMode) {
		isRepeating = true;
		repeatMode = _repeatMode;
	}

	public void noRepeat() {
		repeatTimes = 0;
		isRepeating = false;
	}

	public void setRepeatTimes(int _repeatTimes) {
		repeatTimes = _repeatTimes;
	}

	public void debug() {
		isDebugging = true;
	}

	public void noDebug() {
		isDebugging = false;
	}

	public void onTweenEvent(TweenEvent te) {

	}

	public void addEventListener(TweenEventListener listener) {
		listeners.add(listener);
	}

	public void removeEventListener(TweenEventListener listener) {
		listeners.remove(listener);
	}

	protected synchronized void dispatchEvent(String eventType) {
		TweenEvent te = new TweenEvent(this, eventType);

		Iterator<TweenEventListener> ls = listeners.iterator();

		while (ls.hasNext())
			ls.next().onTweenEvent(te);
	}
}
